#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------


#include "travel.au3"
#include "harvest.au3"
#include "shop.au3"
#include "monster.au3"

Global $last_enter = 0
Func next_over($pos,$ms)
   $nextenter[$pos] = $last_enter + $ms
EndFunc

;Func process_work(ByRef $hWnd, ByRef $Msg, ByRef $iIDTimer, ByRef $dwTime)
Func process_work($hWnd, $Msg, $iIDTimer, $dwTime)
   #forceref $hWnd, $Msg, $iIDTimer, $dwTime
   $last_enter = $dwTime
   If Not check_work() Then Return False
   ;_DebugOut("process " & $dwTime)
   For $pos = 1 To $flag_work[0]
	  If $flag_work[$pos] And $nextenter[$pos] <= $dwTime Then
		 ;_DebugOut("process " & $dwTime & " pos=" & $pos & " step=" & $step[$pos])
		 Switch $step[$pos]
			Case -2
			   ; make move to next
			   If make_move_island($pos) Then
				  message($pos,"wait move")
				  $step[$pos] = -1;
				  $substep[$pos] = 0
				  next_over($pos,200)
			   EndIf
			Case -1
			   ; check for end move
			   If done_move_island($pos) Then
				  message($pos,"move done")
				  $step[$pos] = 0;
				  $substep[$pos] = 0
			   EndIf
			Case 0 To UBound($actions) - 1
			   ; check actions
			   If $need_action[$pos][$step[$pos]] Then
				  ;_DebugOut("call: " & $actions[$step[$pos]][1] & "( " & $pos & ")")
				  If Call($actions[$step[$pos]][1],$pos) Then
					 ;_DebugOut("continue")
					 ContinueLoop
				  EndIf
				  ;_DebugOut("next step")
			   EndIf
			   $step[$pos] += 1
			   $substep[$pos] = 0
			   If $step[$pos] >= UBound($actions) Then
				  $step[$pos] = -2
			   EndIf
		 EndSwitch
	  EndIf
   Next
EndFunc

Func message($pos,$str)
   Local $p = $winlist[$pos]
   GUICtrlSetData($p[$index_label],$str & " p: " & $winprogress[$pos])
EndFunc

Func input($pos)
   Local $p = $winlist[$pos]
   Local $data = GUICtrlRead($p[$index_action + UBound($actions) + $step[$pos]])
   ;_DebugOut("try read('" & $data & "') from " & $p[$index_action + UBound($actions) + $step[$pos]] & " by " & ($index_action + UBound($actions) + $step[$pos]))
   Return $data
EndFunc

Func make_move_island($pos)
   Return make_click($pos,$move[0],$move[1])
EndFunc

Func done_move_island($pos)
   Local $p = $winlist[$pos]
   $substep[$pos] += 1
   ;WinSetTrans($p[$index_handle],"",255)
   ;WinSetState($p[$index_handle],"",@SW_RESTORE)
   ;Sleep(10)
   Local $color = PixelGetColor($p[0] + $p[2] + $move[2],$p[1] + $p[3] + $move[3],$p[$index_handle])
   ;WinSetState($p[$index_handle],"",@SW_MINIMIZE)
   ;WinSetTrans($p[$index_handle],"",30)
   ;_DebugOut("s=" & $substep[$pos] & " " & Hex($color,6) & " cmp " & Hex($move[4],6))
   If $color = $move[4] Then
	  Return True
   Else
	  Return False
   EndIf
EndFunc

Func make_click($pos, $x, $y)
   Local $p = $winlist[$pos]
   ;_DebugOut("click " & $x & " " & $y)
   Local $r = ControlClick($p[$index_handle], "", $control_ff, "left", 1, $x, $y)
   If $r = 0 Then _DebugOut("error")
   Return True
EndFunc

;Global Const $WM_MOUSEMOVE = 0x0200
;Global Const $WM_MOUSEHOVER = 0x02A1

Func make_move($pos,$x,$y)
   Local $r = ControlMessage($p[$index_handle], "", $control_ff, $WM_MOUSEHOVER, $x, $y)
   ;Local $p = $winlist[$pos]
   ;Local $mx = $x + $p[2] + $p[0]
   ;Local $my = $x + $p[3] + $p[1]
   ;_DebugOut("move to (" & $x & "," & $y & ") abs(" & $mx & "," & $my & ")")
   ;DllCall("user32.dll", "int", "SendMessage", "hwnd", $p[$index_handle], "int", $WM_MOUSEHOVER, "int", 0, "long", _MakeLong($mx, $my))
EndFunc

Func _MakeLong($LoWord,$HiWord)
   Return BitOR($HiWord * 0x10000, BitAND($LoWord, 0xFFFF))
EndFunc
